package org.cgs.web.beans;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.Text;

public final class Student implements Comparable<Student>, Cloneable
{
	// Attributes for the Student object
	private String studentNumber;
	private String courseCode;
	private String name;
	private String email;
	private String phone;
	private String programAndYear;
	private String whyInterested;
	private String recentCourse;
	private String likeSubject;
	private String transportation;
	private String teachingAttraction;
	private String teachingExperience;
	private boolean hasVehicle;
	private boolean hasTeachingExperience;
	private boolean hasSubjectKnowledge;
	private boolean hasVehicleSet;
	private boolean hasTeachingExperienceSet;
	private boolean hasSubjectKnowledgeSet;
	private List<String> times;
	private final List<Assignment> assignments;
	private final List<Assignment> assignmentsBackup;

	public Student(String sNumber, String theCourseCode, String studentName, String studentEmail, String studentPhone, String pAndY, String why, String recent, String like, String studentTransportation, String teachExp, String teachAttr, boolean vehicle, boolean teaching, boolean background, List<String> studentTimes)
	{
		studentNumber = sNumber;
		courseCode = theCourseCode;
		name = studentName;
		email = studentEmail;
		phone = studentPhone;
		programAndYear = pAndY;
		whyInterested = why;
		recentCourse = recent;
		likeSubject = like;
		transportation = studentTransportation;
		teachingExperience = teachExp;
		teachingAttraction = teachAttr;
		times = studentTimes;
		hasVehicle = vehicle;
		hasTeachingExperience = teaching;
		hasSubjectKnowledge = background;
		assignments = new ArrayList<Assignment>();
		assignmentsBackup = new ArrayList<Assignment>();
	} // End constructor

	public Student()
	{
		studentNumber = "";
		courseCode = "";
		name = "";
		email = "";
		phone = "";
		programAndYear = "";
		whyInterested = "";
		recentCourse = "";
		likeSubject = "";
		transportation = "";
		teachingExperience = "";
		teachingAttraction = "";
		times = new ArrayList<String>();
		hasVehicle = false;
		hasTeachingExperience = false;
		hasSubjectKnowledge = false;
		assignments = new ArrayList<Assignment>();
		assignmentsBackup = new ArrayList<Assignment>();
	}

	public Student(String sNumber, String theCourseCode, String studentName, String studentEmail, String studentPhone, String pAndY, String why, String recent, String like, String studentTransportation, String teachExp, String teachAttr, boolean vehicle, boolean teaching, boolean background, List<String> studentTimes, List<Assignment> ass)
	{
		this(sNumber, theCourseCode, studentName, studentEmail, studentPhone, pAndY, why, recent, like, studentTransportation, teachExp, teachAttr, vehicle, teaching, background, studentTimes);

		for (Assignment a : ass)
		{
			addAssignment(a);
		} // End for loop

		Collections.shuffle(assignments);
	} // End constructor

	//
	// Get/Set methods
	//
	
	public List<Assignment> getAssignments()
	{
		return assignments;
	} // End method

	public String getCourseCode()
	{
		return courseCode;
	} // End method

	public String getEmail()
	{
		return email;
	} // End method

	public String getLikeSubject()
	{
		return likeSubject;
	} // End method

	public String getName()
	{
		return name;
	} // End method

	public String getPhone()
	{
		return phone;
	} // End method

	public String getProgramAndYear()
	{
		return programAndYear;
	} // End method

	public String getRecentCourse()
	{
		return recentCourse;
	} // End method
	
	
	public String getStudentNumber()
	{
		return studentNumber;
	} // End method

	public String getTeachingAttraction()
	{
		return teachingAttraction;
	} // End method

	public String getTeachingExperience()
	{
		return teachingExperience;
	} // End method

	public List<String> getTimes()
	{
		return times;
	} // End method

	public String getTransportation()
	{
		return transportation;
	} // End method

	public String getWhyInterested()
	{
		return whyInterested;
	} // End method

	public boolean isHasSubjectKnowledge()
	{
		return hasSubjectKnowledge;
	} // End method

	public boolean isHasTeachingExperience()
	{
		return hasTeachingExperience;
	} // End method

	public boolean isHasVehicle()
	{
		return hasVehicle;
	} // End method
	
	public boolean isHasVehicleSet()
	{
		return hasVehicleSet;
	}
	
	public boolean isHasTeachingExperienceSet()
	{
		return hasTeachingExperienceSet;
	}

	public boolean isHasSubjectKnowledgeSet()
	{
		return hasSubjectKnowledgeSet;
	}

	public void setCourseCode(String c)
	{
		courseCode = c;
	} // End method

	public void setEmail(String e)
	{
		email = e;
	} // End method

	public void setHasSubjectKnowledge(boolean sk)
	{
		hasSubjectKnowledge = sk;
	} // End method

	public void setHasTeachingExperience(boolean te)
	{
		hasTeachingExperience = te;
	} // End method

	public void setHasVehicle(boolean v)
	{
		hasVehicle = v;
	} // End method

	public void setLikeSubject(String like)
	{
		likeSubject = like;
	} // End method

	public void setName(String n)
	{
		name = n;
	} // End method

	public void setPhone(String p)
	{
		phone = p;
	} // End method

	public void setProgramAndYear(String pAndY)
	{
		programAndYear = pAndY;
	} // End method

	public void setRecentCourse(String recent)
	{
		recentCourse = recent;
	} // End method

	public void setStudentNumber(String id)
	{
		studentNumber = id;
	} // End method

	public void setTeachingAttraction(String attr)
	{
		teachingAttraction = attr;
	} // End method

	public void setTeachingExperience(String exp)
	{
		teachingExperience = exp;
	} // End method

	public void setTimes(List<String> t)
	{
		times = t;
	} // End method

	public void setTransportation(String t)
	{
		transportation = t;
	} // End method

	public void setWhyInterested(String why)
	{
		whyInterested = why;
	} // End method

	public void setHasVehicleSet(boolean v)
	{
		hasVehicleSet = v;
	} // End method

	public void setHasTeachingExperienceSet(boolean t)
	{
		hasTeachingExperienceSet = t;
	} // End method

	public void setHasSubjectKnowledgeSet(boolean s)
	{
		hasSubjectKnowledgeSet = s;
	} // End method
	
	//
	// Override methods
	//
	
	@Override
	public Object clone()
	{		
		return new Student(studentNumber, courseCode, name, email, phone, programAndYear, whyInterested, recentCourse, likeSubject, transportation, teachingExperience, teachingAttraction, hasVehicle, hasTeachingExperience, hasSubjectKnowledge, times, assignments);
	} // End method

	@Override
	public boolean equals(Object o)
	{
		if (o instanceof Student)
		{
			Student so = (Student) o;
			return so.studentNumber.equals(studentNumber) && so.courseCode.equals(courseCode);
		}

		return false;
	} // End method
	
	@Override
	public int hashCode()
	{
		return studentNumber.hashCode();
	} // End method

	@Override
	public String toString()
	{
		return studentNumber;
	} // End method

	public int compareTo(Student s)
	{
		return studentNumber.compareTo(s.studentNumber);
	} // End method
		
	//
	// Operational methods
	//
	
	public void addAssignment(Assignment a)
	{
		assignments.add(a);
		assignmentsBackup.add(a);
	} // End method
	
	public void addTime(String t)
	{
		times.add(t);
	}

	public boolean canSchoolBeAssignedHere(String time1)
	{
		for (Assignment a : assignmentsBackup)
		{
			if (a.getTime1().equalsIgnoreCase(time1))
			{
				return true;
			} // End if
		} // End for loop

		return false;
	} // End method

	public void removeAssignment(Object a)
	{
		assignments.remove(a);
	} // End method

	public Node getStudentAsXML(Document doc, Element e)
	{
		Element student = doc.createElement("Student");
		student.setAttribute("HasSubjectKnowledge", Boolean.toString(hasSubjectKnowledge));
		student.setAttribute("HasTeachingExperience", Boolean.toString(hasTeachingExperience));
		student.setAttribute("HasVehicle", Boolean.toString(hasVehicle));
		e.appendChild(student);

		Element nameElm = doc.createElement("Name");
		student.appendChild(nameElm);
		Text txtName = doc.createTextNode(name == null ? "" : name);
		nameElm.appendChild(txtName);

		Element studentNumberElm = doc.createElement("StudentNumber");
		student.appendChild(studentNumberElm);
		Text txtStudentNumber = doc.createTextNode(studentNumber == null ? "" : studentNumber);
		studentNumberElm.appendChild(txtStudentNumber);

		Element emailElm = doc.createElement("Email");
		student.appendChild(emailElm);
		Text txtEmail = doc.createTextNode(email == null ? "" : email);
		emailElm.appendChild(txtEmail);

		Element phoneElm = doc.createElement("Phone");
		student.appendChild(phoneElm);
		Text txtPhone = doc.createTextNode(phone == null ? "" : phone);
		phoneElm.appendChild(txtPhone);

		return student;
	} // End method
}
